﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MetroTemplate.Model;

namespace MetroTemplate.ViewModel
{
    public class FacturesViewModel : ViewModelBase,INotifyPropertyChanged
    {

        public const string FacturePropertyName = "Factures";

        private FacturesDataService Service = new FacturesDataService();

        private ObservableCollection<ArticleModel> _Articles
            = new ObservableCollection<ArticleModel>();

        private ObservableCollection<FacturesModel> _Factures;

        public ObservableCollection<FacturesModel> Factures
        {
            get { return _Factures; }
            set
            {
                if (_Factures == value)
                    return;
                _Factures = value;
                RaiseChangedEvent(FacturePropertyName);
            }
        }

        private FacturesModel _SelectedFacture;

        public FacturesModel SelectedFacture
        {
            get
            {
                return _SelectedFacture;
            }
            set
            {
                if (_SelectedFacture != null &&_SelectedFacture.FactureId == value.FactureId)
                    return;
                _SelectedFacture = Factures.Single(Elem => Elem.FactureId == value.FactureId);
                SelectedFacture.PropertyChanged += SelectedFacture_PropertyChanged;
                Articles = SelectedFacture.Articles;
                RaiseChangedEvent("SelectedFacture");
            }
        }

        private ArticleModel _SelectedArticle;

        public ArticleModel SelectedArticle
        {
            get
            {
                return _SelectedArticle;
            }
            set
            {
                if (value == null)
                {
                    return;
                }
                else
                {
                }
                if (_SelectedArticle == value)
                    return;
                _SelectedArticle = value;
                SelectedArticle.PropertyChanged += SelectedArticle_PropertyChanged;
                RaiseChangedEvent("SelectedArticle");
            }
        }

        public ObservableCollection<ArticleModel> Articles
        {
            get
            {
                return _Articles;
            }
            set
            {
                if (_Articles == value)
                    return;
                _Articles = value;
                RaiseChangedEvent("Articles");
            }
        }

        void SelectedArticle_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            MessageBox.Show(e.PropertyName);
        }

        #region Events
        /// <summary>
        /// Permet de déclancher l'évenement PropertyChanged
        /// </summary>
        /// <param name="paramname">Le nom de la propriété modifiée</param>
        private void RaiseChangedEvent(string paramname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(paramname));
            }
        }

        

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        #endregion

        public FacturesViewModel()
        {

            Service.GetData((facture, error) =>
            {
                if (error != null)
                    MessageBox.Show(error.ToString());
                else
                    Factures = new ObservableCollection<FacturesModel>(facture);
            });
            if (Factures != null && Factures.Count > 0)
                SelectedFacture = Factures.ElementAt(0);
            this.PropertyChanged += FacturesViewModel_PropertyChanged;
        }

        void SelectedFacture_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Service.UpdateItem(SelectedFacture, e.PropertyName);
        }

        void FacturesViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
        }
    }
}
